<?php
include_once(dirname(__FILE__)."/../../libs/string/libstr.php");
include_once(dirname(__FILE__)."/../../libs/dbclass/dbconn.php");
require_once(dirname(__FILE__)."/../../libs/dbclass/dbservice.php");
class commentlib extends dbconn
{
	public  $sf=10;
	public  $guestcomment=1;
	public  $guestrcomment=1;
	function  getjifen()
	{
		$mc=new libstr();
		$_userid=$mc->Ismp($_SESSION["userid"]);
		if($mc->Ismp($_SESSION["userid"])=="")
		{
			$_userid=$mc->Ismp($_SESSION["admin_userid"]);
		}
		if($mc->Ismp($_userid)!="")
		{
			$rs=parent::dvstr("select tjifen from member_content where userid='".$_userid."'");
			return $rs["tjifen"];
		}
		return 0;
	}
	function commentsave()
	{
		$mc=new libstr();
		$dbs=new dbservice();
		if($this->guestcomment==0)
		{
			$mc->msg("请您先登录","go");
		}
		if($mc->request("topicname","")==""){$mc->msg("标题不能为空","go");}
		//if($mc->request("userid","")==""){$mc->msg("userid不能为空","go");}
		//if($mc->request("username","")==""){$mc->msg("username不能为空","go");}
		if($mc->request("content","")==""){$mc->msg("内容不能为空","go");}
		if($mc->request("cataid","")==""){$mc->msg("所属频道不能为空","go");}
		if($_POST["fenshu"]>$this->getjifen())
		{
			$mc->msg("您的积分小于所选择的分数","go");
		}

		$nowtime=date("Y-m-d H:i:s");
		$_topicname=$mc->request("topicname","");
		$_userid=$mc->Ismp($_SESSION["userid"]);
		if($mc->Ismp($_SESSION["userid"])=="")
		{
			$_userid=$mc->Ismp($_SESSION["admin_userid"]);
		}
		if($_userid=="")
		{
			$_userid="guest";
		}
		if($_userid=="")
		{
			$_userid="guest";
		}
		if($_userid !="guest")
		{
			$lp=new login();
			if( $lp->getpowera("comment_wen","true")=="true"){
				$mc->msg("你无权限提问");
			}
		}
		$_username=$mc->Ismp($_SESSION["username"]);
		if($mc->Ismp($_SESSION["username"])=="")
		{
			$_username=$mc->Ismp($_SESSION["admin_username"]);
		}
		if($_username=="")
		{
			$_username=$mc->getip();
		}
	
		if($mc->request("hiden","")=="1")
		{
			$_username="guest";

			if($mc->Ismp($_userid)!="guest")
			{
				if($_POST["fenshu"]<$this->sf)
				{
					$mc->msg("因为您是匿名发表，您必须付出".$this->sf."分,但您选择的分数不够");
				}
				parent::cmd("update member_content set tjifen=tjifen-".$_POST["fenshu"]." where userid='".$_userid."'");
			}
		}
		else
		{
			if($mc->Ismp($_userid)!="guest")
			{
				parent::cmd("update member_content set tjifen=tjifen-".$_POST["fenshu"]." where userid='".$_userid."'");
			}
		}
		$_content=$mc->request("content","");
		$_hit=1;
		$_rehit=0;
		$_reid=0;
		$_mid=0;
		$_bestid=0;
		$_isok=0;
		$_istop=0;
		$_fenshu=$mc->request("fenshu","0");
		$_rootid=$dbs->getrootdata("comment_class",$mc->request("cataid","0"));
		$_createtime=$mc->request("createtime",$nowtime);
		$_updatetime=$mc->request("updatetime",$nowtime);
		$_oktime=$mc->request("oktime",$nowtime);
		$_retime=$mc->request("retime",$nowtime);
		$_ip=$mc->getip();
		$_isshow=1;



		$sql="insert into comment_content(topicname,userid,username,content,hit,rehit,reid,mid,bestid,isok,istop,fenshu,rootid,createtime,updatetime,oktime,retime,ip,isshow,lastusername,okusername)values
		('".$_topicname."','".$_userid."','".$_username."','".$_content."',".$_hit.",'".$_rehit."',".$_reid.",".$_mid.",".$_bestid.",".$_isok.",".$_istop.",".$_fenshu.",'".$_rootid."','".$_createtime."','".$_updatetime."','".$_oktime."','".$_retime."','".$_ip."',".$_isshow.",' ', ' ')";
		//echo $sql;
		parent::cmd($sql);
		return true;
	}
	function  commentrsave($_reid)
	{
		$mc=new libstr();
		$dbs=new dbservice();
		if($this->guestrcomment==0)
		{
			$mc->msg("请您先登录","go");
		}
		//	if($mc->request("topicname","")==""){$mc->msg("标题不能为空","go");}
		//if($mc->request("userid","")==""){$mc->msg("userid不能为空","go");}
		//if($mc->request("username","")==""){$mc->msg("username不能为空","go");}
		if($mc->request("content","")==""){$mc->msg("内容不能为空","go");}
		//	if($mc->request("cataid","")==""){$mc->msg("所属频道不能为空","go");}
		if($_POST["fenshu"]>$this->getjifen())
		{
			//	$mc->msg("您的积分小于所选择的分数","go");
		}

		$nowtime=date("Y-m-d H:i:s");
		$rstx=$dbs->dvstr("select topicname,isok from comment_content where id='".$_reid."'");
		if($rstx["isok"]==1)
		{
			$mc->msg("该话题已经结贴");
		}
		$_topicname=$mc->request("topicname",$rstx["topicname"]);
		$_userid=$mc->Ismp($_SESSION["userid"]);
		if($mc->Ismp($_SESSION["userid"])=="")
		{
			$_userid=$mc->Ismp($_SESSION["admin_userid"]);
		}

		if($_userid=="")
		{
			$_userid="guest";
		}
		if($_userid !="guest")
		{
			$lp=new login();
			if( $lp->getpowera("comment_da","true")=="true"){
				$mc->msg("你无权限回复");
			}
		}
		$_username=$mc->Ismp($_SESSION["username"]);
		if($mc->Ismp($_SESSION["username"])=="")
		{
			$_username=$mc->Ismp($_SESSION["admin_username"]);
		}
		if($_username=="")
		{
			$_username=$mc->getip();
		}
		if($mc->request("hiden","")=="1")
		{
			$_username="guest";

			if($mc->Ismp($_userid)!="")
			{
				//	parent::cmd("update member_content set tjifen=tjifen-".$_POST["fenshu"]." where userid='".$_SESSION["userid"]."'");
			}
		}
		else
		{
			if($mc->Ismp($_SESSION["userid"])!="")
			{
				//parent::cmd("update member_content set tjifen=tjifen-".$this->sf." where userid='".$_SESSION["userid"]."'");
			}
		}
		$_content=$mc->request("content","");
		$_hit=1;
		$_rehit=0;
		//$_reid=0;
		$_mid=1;
		$_bestid=0;
		$_isok=0;
		$_istop=0;
		$_fenshu=$mc->request("fenshu","0");
		//$_rootid=$dbs->getrootdata("comment_class",$mc->request("cataid","0"));
		$_rootid=" ";
		$_createtime=$mc->request("createtime",$nowtime);
		$_updatetime=$mc->request("updatetime",$nowtime);
		$_oktime=$mc->request("oktime",$nowtime);
		$_retime=$mc->request("retime",$nowtime);
		$_ip=$mc->getip();
		$_isshow=1;
		parent::cmd("update comment_content set rehit=rehit+1,lastusername='".$_username."',retime='".$nowtime."' where id='".$_reid."'");
		$sql="insert into comment_content(topicname,userid,username,content,hit,rehit,reid,mid,bestid,isok,istop,fenshu,rootid,createtime,updatetime,oktime,retime,ip,isshow,lastusername,okusername)values
		('".$_topicname."','".$_userid."','".$_username."','".$_content."',".$_hit.",'".$_rehit."',".$_reid.",".$_mid.",".$_bestid.",".$_isok.",".$_istop.",".$_fenshu.",'".$_rootid."','".$_createtime."','".$_updatetime."','".$_oktime."','".$_retime."','".$_ip."',".$_isshow.",' ', ' ')";
		//echo $sql;
		parent::cmd($sql);
		return true;
	}
	function deletecomment($id)
	{
		$mc=new libstr();
		$lp=new login();
		if( $lp->getpowera("comment_delete","true")=="true"){
			$mc->msg("你无权限删除");
		}

		if($mc->Ismp($id)=="")
		{
			return false;
		}
		$rsget=parent::ds("select * from comment_content where mid=0 and  id='".$id."'");
		foreach ($rsget as $rs)
		{
			parent::cmd("delete from comment_content where mid=1 and reid='".$rs["id"]."'");
		}
		parent::cmd("delete from comment_content where mid=0 and id='".$id."'");
		return true;

	}
	function deletercomment($id)
	{
		$mc=new libstr();
		$lp=new login();
		if( $lp->getpowera("comment_delete","true")=="true"){
			$mc->msg("你无权限删除");
		}
		if($mc->Ismp($id)=="")
		{
			return false;
		}
		$rs=parent::dvstr("select reid from comment_content where mid=1 and id='".$id."'");
		parent::cmd("update comment_content set rehit=rehit-1 where mid=0 and id='".$rs["reid"]."'");
		parent::cmd("delete from comment_content where mid=1 and id='".$id."'");
		return true;
	}
	function isbest($id,$mainid)
	{
		$mc=new libstr();
		if($mc->Ismp($id)=="")
		{
			return false;
		}
		if($mc->Ismp($mainid)=="")
		{
			return false;
		}
		$rs=parent::dvstr("select username,userid,isok,createtime from comment_content where id='".$id."'");
		$rs1=parent::dvstr("select isok,userid from comment_content where id='".$mainid."'");
		$_userid=$mc->Ismp($_SESSION["userid"]);
		if($mc->Ismp($_SESSION["userid"])=="")
		{
			$_userid=$mc->Ismp($_SESSION["admin_userid"]);
		}
		$_where="";
		if($_userid!=$rs1['userid'])
		{
			$lp=new login();
			if( $lp->getpowera("comment_shenhe","true")=="true"){
				$mc->msg("你无权限");
			}
		}else
		{
			$_where=" userid= '".$_userid."' and ";
		}

		if($rs1["isok"]=="1")
		{
			$mc->msg("该话题已设置了最佳答案","go");
		}
		parent::cmd("update comment_content set bestid=".$id.",okusername='".$rs["username"]."',isok=1,oktime='".trim($rs["createtime"])."' where ".$_where." mid=0 and id='".$mainid."'");
		parent::cmd("update comment_content set bestid=0 where mid=1 and reid='".$mainid."'");
		parent::cmd("update comment_content set bestid=1 where mid=1 and id='".$id."'");
		return  true;
	}
	function pengbi($id,$cansh=0)
	{
		$mc=new libstr();
		if($mc->Ismp($id)=="")
		{
			return false;
		}
		$lp=new login();
		if( $lp->getpowera("comment_shenhe","true")=="true"){
			$mc->msg("你无权限屏蔽");
		}

		parent::cmd("update comment_content set isshow=".$cansh." where id='".$id."'");
		return  true;
	}
	function showcontent($content,$isshow)
	{
		if($isshow==0)
		{
			return "内容已被屏蔽..";
		}
		return $content;
	}
	function niming($name)
	{
			if(trim($name)=="")
		{
			return "尚无";
		}
		if(ereg("guest",$name)==1)
		{
			return "匿名";
		}
		return  $name;
	}
	function niming1($name)
	{
		if(trim($name)=="")
		{
			return "尚无";
		}
		$tx=split("\.",$name);
		if(count($tx)>1)
		{
			return "匿名";
		}
		return  $name;
	}
	function nimingip($ip)
	{
		if($ip=="")
		{
			return $ip;
		}
		$tx=split("\.",$ip);

		if(count($tx)==4)
		{
			return $tx[0].".".$tx[1].".*.*";
		}
		return "-";
	}
	function  edit($str)
	{
		$mc=new libstr();
		$lp=new login();
		$rs=parent::dvstr("select userid from comment_content where id='".$str."'");
		$_userid=$mc->Ismp($_SESSION["userid"]);
		if($mc->Ismp($_SESSION["userid"])=="")
		{
			$_userid=$mc->Ismp($_SESSION["admin_userid"]);
		}
		$_where="";
		if(trim($rs['userid'])!=$_userid)
		{
			if( $lp->getpowera("comment_edit","true")=="true"){
				$mc->msg("你无权限编辑","javascript:;window.close();");
			}
		}else
		{
			$_where=" userid='".$_userid."' and ";
		}
		if(trim($mc->Ismp($_POST["content"]))=="")
		{
			$mc->msg("内容不能为空");
		}
		parent::cmd("update comment_content set content='".$_POST['content']."' where ".$_where." id='".$str."'");
		return true;
	}
	function sanfen($id,$reid)
	{
		$mc=new libstr();
		$lp=new login();
		$_userid=$mc->Ismp($_SESSION["userid"]);

		if($mc->Ismp($_SESSION["userid"])=="")
		{
			$_userid=$mc->Ismp($_SESSION["admin_userid"]);
		}
		if($_userid=="")
		{
			$mc->msg("您请先登录","javascript:;window.close();");
		}


		$rs=parent::dvstr("select fenshu,userid from comment_content where id='".$id."'");
		if(trim($rs['userid'])!=$_userid)
		{
			if( $lp->getpowera("comment_shenhe","true")=="true"){
				$mc->msg("你无权限给分","javascript:;window.close();");
			}
		}

		$rers=parent::dvstr("select fenshu,userid from comment_content where id='".$reid."'");
		$ckstr=parent::dvstr("select count(id) as ccc from member_content where userid='".trim($rers["userid"])."'");
		if($ckstr['ccc']==0)
		{
			$mc->msg("不存在会员".trim($rers["username"])."或者已经被删除","javascript:;window.close();");
		}
		if($_POST['fenshu']>$rs['fenshu'])
		{
			$mc->msg("您输入的分数大于当前帖子的分数","go");
		}
		parent::cmd("update member_content set tjifen=tjifen+".$_POST['fenshu']." where userid='".trim($rers["userid"])."'");
		parent::cmd("update comment_content set fenshu=fenshu+".$_POST['fenshu']." where id='".trim($reid)."'");
		parent::cmd("update comment_content set fenshu=fenshu-".$_POST['fenshu']." where id='".trim($id)."'");
		$mc->msg("操作成功","javascript:;opener.location.reload();window.close();");
	}
	function showmemberadmin($userid)
	{
		$lp=new login();
		if($lp->getpowera("comment_shenhe","true")!="true" || $_SESSION["userid"]==$userid ||  $_SESSION["admin_userid"]==$userid)
		{
			return true;
		}
		return false;
	}
	function fensize($str)
	{
		$mc=new libstr();
		if($mc->Ismp($_POST["btn"]))
		{

		}
	}
}
?>